<?php

namespace Addons\OaSystem\Model;
use Think\Model;

/**
 * OaSystem模型
 */
class OaSysLogModel extends Model{
    public $operationList = array(
        array('OaEmployee', 'add', '新增员工信息'),
        array('OaEmployee', 'import', 'excel批量导入员工信息'),
        array('OaEmployee', 'export', 'excel导出员工信息'),
        array('OaEmpAttendance', 'attendancelists', '查看员工考勤列表'),
        array('OaEmpAttendance', 'overtimelists', '查看员工加班列表'),
        array('OaWorkTimeSetting', 'import', '工作时间导入'),
        array('OaDept', 'add', '新增部门'),
        array('OaDuty', 'add', '新增职务'),
        array('OaDuty', 'del', '删除职务'),
        array('OaRole', 'add', '新增角色'),
        array('OaRole', 'del', '删除角色'),
        array('OaRoleDuty', 'add', '新增职务角色关系'),
        array('OaRoleDuty', 'del', '删除职务角色关系'),
        array('OaUserRole', 'add', '新增员工角色关系'),
        array('OaUserRole', 'del', '删除员工角色关系'),
        array('OaNode', 'add', '新增功能模块'),
        array('OaEmpAdminLevel', 'add', '新增行政级别'),
        array('OaEmpAdminLevel', 'del', '删除行政级别'),
        array('OaApplication', 'add', '新建报销'),
        array('OaApplication', 'approveApplication', '审批报销'),
        array('OaLogisticsService', 'foodimport', '菜品excel导入'),
        array('OaLogisticsService', 'foodexport', '菜品导出'),
        array('OaLogisticsService', 'add', '新增商品'),
        array('OaLogisticsService', 'del', '删除商品'),
        array('OaLogisticsService', 'goodsimport', '商品excel导入'),
        array('OaLogisticsService', 'goodsexport', '商品导出')
    );

    public function addLog($empid, $controller, $action){
        if(empty($empid) || empty($controller) || empty($action))
            return;
        foreach ($this->operationList as $v) {
            if(strtolower($controller) == strtolower($v[0]) &&
                strtolower($action) == strtolower($v[1])) {
                $operation = $v[2];
                break;
            }
        }
        if(empty($operation))
            return;
        $data['appid'] = get_app_info()['appid'];
        $data['emp_id'] = $empid;
        $data['time'] = time();
        $data['operate'] = $operation;
        $data['controller'] = $controller;
        $data['action'] = $action;

        $nodes = db_select('oa_node');
        foreach ($nodes as $node) {
            $nodeUrl = FUNC_BASE_URL.WPID.'/'.$node['url'];
            if ($nodeUrl === $_SERVER["REQUEST_URI"]) {
                $data['node_id'] = $node['id'];
                $data['operate'] = $node['name'];
                break;
            }
        }

        M('oa_sys_log')->add($data);
    }
}
